﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using papercut2.Data.Global;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;


namespace papercut2.Data.ImageProcess
{
    class GetBorder
    {
        private static Bitmap pic  ;
        private static Hashtable BP=new Hashtable ();

        public static Bitmap Pic
        {
            get
            {
                return pic;
            }
            set
            {
                pic = value;
            }
        }
        public static Hashtable Bp
        {
            get
            {
                return BP;
            }
            set
            {
                BP = value;
            }
        }
        //put a point into BP
        public static void put(bpoint temp)
        {
            String keystr =  temp.X.ToString() + "#" + temp.Y.ToString();
            if (!isContain((int)temp.X,(int) temp.Y))
            {
                BP.Add(keystr, temp);
            }
        }
        //get value by point,but it needs to check whether object is in BP
        public static bpoint get(int x, int y)
        {
            String keystr =  x.ToString() + "#" + y.ToString();
            bpoint temp = (bpoint)BP[keystr];
            return temp;
        }

        public static void remove(bpoint temp)
        {
            String keystr =temp.X.ToString() + "#" + temp.Y.ToString() ;
            if (isContain((int)temp.X,(int)temp.Y))
            {
                BP.Remove(keystr);
            }
        }
        // to see whether Point(x,y) is in BP
        public static Boolean isContain(int x, int y)
        {
            String keystr = x.ToString() + "#" + y.ToString();
            
            if (BP.Contains(keystr))
                return true;
            else
                return false;
        }
        public static Bitmap getBorderPicture()
        {
            int width = pic.Width;
            int height =pic.Height;
            int low = GlobalData.Low;
            int high = GlobalData.High;
            Bitmap temp = new Bitmap(width, height);

            int[,] P = new int[width, height];
            int i, j;

            for (i = 1; i < width - 1; i++)
            {
                for (j = 1; j < height - 1; j++)
                {
                    Color cc = pic.GetPixel(i, j);
                    int rr = cc.R;
                    int gg = cc.G;
                    int bb = cc.B;
                    if (rr >= low && rr <= high && gg >= low && gg <= high && bb >= low && bb <= high)
                    {
                        P[i, j] = 1;
                    }
                    else
                    {
                        P[i, j] = 0;
                    }
                }
            }
            int[] oper = { 0, 0, 0, 0 };
            int[] Neighoor = new int[4];
            
            int k;
            for (i = 2; i < width - 2; i++)
            {
                for (j = 2; j < height - 2; j++)
                {
                    Boolean Is_Boundary = false;
                    if (P[i, j] == 1)
                    {
                        Neighoor[0] = P[i + 1, j];
                        Neighoor[1] = P[i, j - 1];
                        Neighoor[2] = P[i - 1, j];
                        Neighoor[3] = P[i, j + 1];
                        for (k = 0; k < 4 && Is_Boundary == false; k++)
                        {
                            if (Neighoor[k] == oper[k])
                                Is_Boundary = true;

                        }
                        if (Is_Boundary == true)
                        {
                            bpoint temppoint = new bpoint(j, i, 0, 0, null);
                            try
                            {
                                temp.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            }
                            catch (Exception e)
                            { }
                            if (!isContain(j, i))
                            {
                                put(temppoint);
                            }
                            //........use hashtable to get the index........
                            //step++;
                        }
                    }
                }
            }


      /*      IDictionaryEnumerator enumerator = BP.GetEnumerator(); //.GetEnumerator();
            Color black = Color.FromArgb(0, 0, 0);
            while (enumerator.MoveNext())
            {
                //System.Windows.Forms.MessageBox.Show(enumerator.Key.ToString());
                bpoint p = (bpoint)enumerator.Value;
                temp.SetPixel(p.Y, p.X, black);
            }
*/
            pic = temp;
            return temp;

        }
 
    }
}
